class ApplicantController < ApplicationController

  def home

  end #end home

  def show_open_positions
    require 'net/http'
    source = 'http://140.211.68.130/GetOpenPositions'
    resp = Net::HTTP.get_response(URI.parse(source))
    data = resp.body
    result = JSON.parse(data)

    @full = result['positions']

  end #end show_open_positions

  def apply
    @post_id = params[:post_id]
    @position_id = params[:position_id]
    hashStr = params[:appFields].to_json
    puts hashStr
  end #end apply

  def start_questionnaire
    require 'net/http'
    require 'json'
    @questions = Array.new

    app = Hash.new

    app    = {
        :first => params[:first],
        :last => params[:last],
        :middle => params[:middle],
        :phone => params[:phone],
        :street => params[:street],
        :city => params[:city],
        :state => params[:state],
        :zip => params[:zip],
        :ssn => params[:ssn],
        :dob => params[:dob],
        :post_id => params[:post_id],
        :position_id => params[:position_id],
        :transport => params[:transport],
        :legal => params[:legal],
        :felon => params[:felon]
    }
    @appJ = app.to_json

    url = 'http://140.211.68.130/GetQuestions?answers=1'
    source = URI.parse(url)
    resp = Net::HTTP.post_form(source, 'position_type_id' => app[:position_id])
    data = resp.body.to_s
    result = JSON.parse(data)
    all = result['questions']

    # Storing questions/answers ONLY for the Kiosk questionnaire (application_state = 1)
    all.each do |question|
      if( question['application_state'] == '1' )
        @questions.push(question)
      end #end if
    end #end loop

    session[:questions] = @questions

  end #end start_questionnaire

  def app_submit
    require 'json'
    require 'net/http'

    questions = session[:questions]
    applicantInfo = params[:app]
    application = Hash.new
    answerArray = Array.new
    application['application_data'] = applicantInfo
    numQuestions = 0.0
    numCorrect = 0.0
    numMultChoice = 0.0
    percentageThreshold = 0.6

    params.each do |key, value|
      if( Integer(key) != nil rescue false )
        if ( Integer(value) != nil rescue false )
          answerArray.push({ 'position_question_id' => key, 'question_option_id' => value })
        else
          answerArray.push({ 'position_question_id' => key, 'answer_text' => value })
        end
      end
    end #end loop

    application['answers'] = answerArray.to_json

# KEEP just in case something is bugged with the session stuff...
=begin
    url = 'http://140.211.68.130/GetQuestions?answers=1'
    source = URI.parse(url)
    resp = Net::HTTP.post_form(source, 'position_type_id' => id)
    data = resp.body.to_s
    result = JSON.parse(data)
    @questions = result['questions']
=end

    questions.each do |question|
      if( question['type'] == 'multiple_choice' )
        question['answers'].each do |answer|
          if ( (answer['question_option_id'] == answerArray[numQuestions]['question_option_id']) && answer['correct'] == '1' )
            numCorrect += 1.0
          end #end if
        end #end answers loop
        numMultChoice += 1.0
      end #end if
      numQuestions += 1.0
    end #end questions loop

    if (numQuestions > 0.0)
      percentageCorrect = numCorrect / numMultChoice

      if( percentageCorrect >= percentageThreshold )
        application['application_state'] = '1'
      else
        application['application_state'] = '0'
      end
    end

    source = 'http://140.211.68.130/SetApplication'
    resp = Net::HTTP.post_form(URI.parse(source), application)
  end #end app_submit

end #end ApplicantController
